home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / zm16src / sz.c < prev    next >
C/C++ Source or Header  |  1988-05-18  |  32KB  |  1,543 lines

  1. /*
  2.  *                ACKNOWLEDGEMENTS
  3.  *
  4.  *    ZMDM was derived from rz/sz for Unix  posted by 
  5.  *    Chuck Forsberg (...!tektronix!reed!omen!caf ). We
  6.  *    thank him for his excellent code, and for giving
  7.  *    us permission to use and distribute his code and
  8.  *    documentation.
  9.  *
  10.  *    Atari St version by:
  11.  *        Jwahar Bammi
  12.  *            usenet: mandrill!bammi@{decvax,sun}.UUCP
  13.  *            csnet:  bammi@mandrill.ces.CWRU.edu
  14.  *            arpa:   bammi@mandrill.ces.CWRU.edu
  15.  *            CompuServe: 71515,155
  16.  */
  17.  
  18. #include "config.h"
  19. #define SVERSION "sz 1.23 01-15-87"
  20. #define SSTVERSION "sz 1.01 03-07-87"
  21. #define OS    "Unix V7/BSD"
  22.  
  23. #ifndef STANDALONE
  24. #define RETURN return
  25. #else
  26. int bibi() {} /* dummy */
  27. #endif 
  28.  
  29. /* #define SDEBUG */
  30.  
  31. /*
  32.  * sz.c By Chuck Forsberg
  33.  *
  34.  *    cc -O sz.c -o sz        USG (SYS III/V) Unix
  35.  *     cc -O -DV7  sz.c -o sz        Unix Version 7, 2.8 - 4.3 BSD
  36.  *
  37.  *        define CRCTABLE to use table driven CRC
  38.  *
  39.  *  ******* Some systems (Venix, Coherent, Regulus) do not *******
  40.  *  ******* support tty raw mode read(2) identically to    *******
  41.  *  ******* Unix. ONEREAD must be defined to force one     *******
  42.  *  ******* character reads for these systems.           *******
  43.  *
  44.  * A program for Unix to send files and commands to computers running
  45.  *  Professional-YAM, PowerCom, YAM, IMP, or programs supporting Y/XMODEM.
  46.  *
  47.  *  Sz uses buffered I/O to greatly reduce CPU time compared to UMODEM.
  48.  *
  49.  *  USG UNIX (3.0) ioctl conventions courtesy Jeff Martin
  50.  *
  51.  *    St v 1.01
  52.  *        added support for 32 bit CRC's (Zmodem) ++jrb
  53.  *
  54.  */
  55.  
  56. #include "zmdm.h"
  57. #include "common.h"
  58. #include "zmodem.h"
  59.  
  60. #ifndef Vsync             /* Atari forgot these in osbind.h */
  61. #define Vsync()    xbios(37)
  62. #endif
  63.  
  64. #ifndef Supexec
  65.         /* Some versions of osbind don't define Supexec */
  66. #define Supexec(X) xbios(38,X)
  67. #endif
  68.  
  69. #define SLOGFILE "szlog"
  70.  
  71. #define purgeline()    while(Bconstat(1)) Bconin(1)
  72. #define S_IFDIR 0x0010
  73.  
  74. /*
  75.  * Attention string to be executed by receiver to interrupt streaming data
  76.  *  when an error is detected.  A pause (0336) may be needed before the
  77.  *  ^C (03) or after it.
  78.  */
  79. #ifdef READCHECK
  80. char Myattn[] = { 0 };
  81. #else
  82. #ifdef USG
  83. char Myattn[] = { 03, 0336, 0 };
  84. #else
  85. char Myattn[] = { 0 };
  86. #endif
  87. #endif
  88.  
  89. #if (MWC || MANX)
  90. FILE *fopen();
  91. #else
  92. FILE *fopen(), *fopenb();
  93. #endif
  94. static unsigned long SaveIntr;
  95. static int Resuming, ForceBin;
  96. static int in;
  97.  
  98. /* called by signal interrupt or terminate to clean things up */
  99. bibis(n)
  100. int n;
  101. {
  102.     canit(); flush_modem(); mode(0);
  103.     fprintf(STDERR, "\r\nsz: caught signal %d; exiting\n", n);
  104.  
  105.     aexit(128+n);
  106. }
  107.  
  108. /* Called when Zmodem gets an interrupt (^X) */
  109. #ifdef ONINTR
  110. onintr()
  111. {
  112.     siggi = 0;
  113.     longjmp(intrjmp, -1);
  114. }
  115. #endif
  116.  
  117. #define ZKER
  118. int Zctlesc;    /* Encode control characters */
  119.  
  120. #ifdef STANDALONE
  121. int main(argc, argv)
  122. #else
  123. int dosz(argc, argv)
  124. #endif
  125. int argc;
  126. char **argv;
  127. {
  128.     register char *cp;
  129.     register int npats;
  130.     int agcnt; char **agcv;
  131.     char **patts;
  132.  
  133. #ifdef STANDALONE
  134. #if (MWC || MANX)
  135.     extern char *lmalloc();
  136. #endif
  137.  
  138.     /* Set up Dta */
  139.     Fsetdta(&statbuf);
  140.  
  141.     /* Get screen rez */
  142.     rez = Getrez();
  143.     drv_map = Drvmap();
  144.  
  145. #if (MWC || MANX)
  146. #ifndef DYNABUF
  147.     if((bufr = (unsigned char *)lmalloc((unsigned long)BBUFSIZ))
  148.                      == (unsigned char *)NULL)
  149. #else
  150.     if((bufr = dalloc()) == (unsigned char *)NULL)
  151. #endif /* DYNABUF */
  152.     {
  153. #ifdef REMOTE
  154.         Bauxws("Sorry, could not allocate enough memory\r\n");
  155. #else
  156.         Bconws("Sorry, could not allocate enough memory\r\n");
  157. #endif
  158.  
  159.         Pterm(4);
  160.     }
  161. #else /* MWC || MANX */
  162. #ifdef DYNABUF
  163.     if((bufr = dalloc()) == (unsigned char *)NULL)
  164.     {
  165. #ifdef REMOTE
  166.         Bauxws("Sorry, could not allocate enough memory\r\n");
  167. #else
  168.         Bconws("Sorry, could not allocate enough memory\r\n");
  169. #endif
  170.         Pterm(5);
  171.     }
  172. #endif /* DYNABUF */
  173. #endif /* MWC || MANX */
  174.  
  175. #ifndef REMOTE
  176.     STDERR = stderr;
  177. #else
  178. #ifndef DLIBS
  179.     if((STDERR = fopen("aux:", "rw")) == (FILE *)NULL)
  180.     {
  181.         Bauxws("Could not Open Aux Stream for Stderr\r\n");
  182.         finish();
  183.     }
  184.     setbuf(STDERR, (char *)NULL);
  185. #else
  186.     STDERR = stdaux;
  187. #endif /* DLIBS */
  188.     
  189. #endif /* REMOTE */
  190.     {
  191.         int speed;
  192.         speed = getbaud();
  193.         Baudrate = BAUD_RATE(speed);
  194.         SetIoBuf();
  195.         Rsconf(speed, 0,-1,-1,-1,-1);
  196.         Vsync(); Vsync();
  197.     }
  198. #endif /* STANDALONE */
  199.  
  200.     SendType = 1;
  201.     Rxtimeout = 600;
  202.     npats=0;
  203.     if (argc<2)
  204.     {
  205.         susage();
  206.         RETURN(1);
  207.     }
  208.  
  209.     initz();
  210. #ifndef STANDALONE
  211.     schkinvok(argv[0]);
  212. #else
  213.     Progname = "sz";
  214. #endif
  215.  
  216.     SaveIntr = Setexc(0x0102, -1L);
  217.     BusErr   = Setexc(2, -1L);
  218.     AddrErr  = Setexc(3, -1L);
  219.  
  220.     Verbose = 0;
  221.     Resuming = FALSE;
  222.     ForceBin = FALSE;
  223.     in = (-1);
  224.     vdebug = 0;
  225.  
  226. #ifdef SDEBUG
  227.     logf = (FILE *)NULL;
  228. #endif
  229.     while (--argc) {
  230.         cp = *++argv;
  231.         if (*cp++ == '-' && *cp) {
  232.             while ( *cp) {
  233.                 switch(*cp++) {
  234.                 case '+':
  235.                     Lzmanag = ZMAPND; break;
  236. #ifdef CSTOPB
  237.                 case '2':
  238.                     Twostop = TRUE; break;
  239. #endif
  240.                 case '7':
  241.                     Wcsmask=0177; break;
  242.  
  243. /*
  244.     On the St we look up the ext and decide. For Xmodem
  245.     transfers, the file is always sent in binary mode
  246.     and it is the responsibility of the receiver to
  247.     strip CR if so desired.
  248.                 case 'a':
  249.                     Lzconv = ZCNL;
  250.                     Ascii = TRUE; break;
  251.                 case 'b':
  252.                     Lzconv = ZCBIN; break;
  253. */
  254.  
  255. /*  ST extention, force binary, useful to back up every thing
  256.  *  in image mode, see -B option of rz too +jrb
  257.  */
  258.                 case 'B':
  259.                     ForceBin = TRUE;
  260.                     Lzconv = ZCBIN;
  261.                     break;
  262.                 case 'C':
  263.                     if (--argc < 1) {
  264.                         susage();
  265.                         RETURN(1);
  266.                     }
  267.                     Cmdtries = atoi(*++argv);
  268.                     break;
  269.                 case 'i':
  270.                     Cmdack1 = ZCACK1;
  271.                     /* **** FALL THROUGH TO **** */
  272.                 case 'c':
  273.                     if (--argc != 1) {
  274.                         susage();
  275.                         RETURN(1);
  276.                     }
  277.                     Command = TRUE;
  278.                     Cmdstr = *++argv;
  279.                     break;
  280.                 case 'd':
  281.                     ++Dottoslash;
  282.                     /* **** FALL THROUGH TO **** */
  283.                 case 'f':
  284.                     Fullname=TRUE; break;
  285.                 case 'E':
  286.                     Zctlesc = (-1); break;
  287.                 case 'e':
  288.                     Zctlesc = 1; break;
  289.                 case 'k':
  290.                     Blklen=KSIZE; break;
  291.                 case 'L':
  292.                     if (--argc < 1) {
  293.                         susage();
  294.                         RETURN(1);
  295.                     }
  296.                     blkopt = atoi(*++argv);
  297.                     if (blkopt<32 || blkopt>1024)
  298.                     {
  299.                         susage();
  300.                         RETURN(1);
  301.                     }
  302.                     break;
  303.                 case 'l':
  304.                     if (--argc < 1) {
  305.                         susage();
  306.                         RETURN(1);
  307.                     }
  308.                     Tframlen = atoi(*++argv);
  309.                     if (Tframlen<32 || Tframlen>1024)
  310.                     {
  311.                         susage();
  312.                         RETURN(1);
  313.                     }
  314.                     break;
  315.                 case 'N':
  316.                     Lzmanag = ZMDIFF;  break;
  317.                 case 'n':
  318.                     Lzmanag = ZMNEW;  break;
  319.                 case 'o':
  320.                     Wantfcs32 = FALSE; break;
  321.                 case 'p':
  322.                     Lzmanag = ZMPROT;  break;
  323.                 case 'r':
  324.                     Lzconv = ZCRESUM; Resuming = TRUE; break;
  325.                 case 'q':
  326.                     Quiet=TRUE; Verbose=0; break;
  327.                 case 't':
  328.                     if (--argc < 1) {
  329.                         susage();
  330.                         RETURN(1);
  331.                     }
  332.                     Rxtimeout = atoi(*++argv);
  333.                     if (Rxtimeout<10 || Rxtimeout>1000)
  334.                     {
  335.                         susage();
  336.                         RETURN(1);
  337.                     }
  338.                     break;
  339. #ifdef TESTATTN
  340.                 case 'T':
  341.                     Testattn = TRUE; break;
  342. #endif
  343.                 case 'u':
  344.                     ++Unlinkafter; break;
  345.                 case 'v':
  346.                     ++Verbose; break;
  347.                 case 'X':
  348.                     ++Modem; break;
  349.                 case 'y':
  350.                     Lzmanag = ZMCLOB; break;
  351.                 default:
  352.                     susage();
  353.                     RETURN(1);
  354.                 }
  355.             }
  356.         }
  357.         else if ( !npats && argc>0) {
  358.             if (argv[0][0]) {
  359.                 npats=argc;
  360.                 patts=argv;
  361.             }
  362.         }
  363.     }
  364.     if (npats < 1 && !Command) 
  365.     {
  366.         susage();
  367.         RETURN(1);
  368.     }
  369.  
  370. #ifdef SDEBUG
  371.     if (Verbose > 2)
  372.     {
  373.         if ((logf = fopen(SLOGFILE, "a"))== (FILE *)NULL)
  374.         {
  375.             fprintf(STDERR, "Can't open log file %s\n",SLOGFILE);
  376.             RETURN(0200);
  377.         }
  378.         fprintf(logf, "Progname=%s\n", Progname);
  379.         vdebug = 1;
  380.         fflush(logf);
  381.     }
  382. #endif
  383.  
  384.     if ( !Quiet)
  385.     {
  386.         if (Verbose < 2)
  387.             Verbose = 2;
  388.     }
  389.  
  390.  
  391.     Setexc(0x0102, bibis);
  392.     Setexc(2, buserr);
  393.     Setexc(3, addrerr);
  394.  
  395.     if(setjmp(busjmp))
  396.     {
  397.         /* On a bus error - instead of 2 bombs */
  398.         fprintf(STDERR,"\r\nFATAL: Bus Error\n\n");
  399. #ifdef SDEBUG
  400.         if(logf != (FILE *)NULL)
  401.             fclose(logf);
  402. #endif
  403.         if(in != -1)
  404.         {
  405.             stfclose(in);
  406.             in = (-1);
  407.         }
  408.         canit();
  409.         Setexc(2, BusErr);
  410.         Setexc(3, AddrErr);
  411.         Setexc(0x0102, SaveIntr);
  412.         RETURN(2);
  413.     }
  414.  
  415.     if(setjmp(addrjmp))
  416.     {
  417.         /* On address error - instead of 3 bombs */
  418.         fprintf(STDERR,"\r\nFATAL: Address Error\n\n");
  419. #ifdef SDEBUG
  420.         if(logf != (FILE *)NULL)
  421.             fclose(logf);
  422. #endif
  423.         if(in != -1)
  424.         {
  425.             stfclose(in);
  426.             in = (-1);
  427.         }
  428.         canit();
  429.         Setexc(2, BusErr);
  430.         Setexc(3, AddrErr);
  431.         Setexc(0x0102, SaveIntr);
  432.         RETURN(3);
  433.     }
  434.  
  435.     if((Exitcode = setjmp(abrtjmp)))
  436.     {
  437.         fprintf(STDERR,"\nTransfer ABORT\n\n");
  438. #ifdef SDEBUG
  439.     if(logf != (FILE *)NULL)
  440.         fclose(logf);
  441. #endif
  442.         if(in != -1)
  443.         {
  444.             stfclose(in);
  445.             in = (-1);
  446.         }
  447.         Setexc(2, BusErr);
  448.         Setexc(3, AddrErr);
  449.         Setexc(0x0102, SaveIntr);
  450.         RETURN(Exitcode);
  451.     }
  452.  
  453.     mode(1);
  454.  
  455.  
  456.     if ( !Modem) {
  457.         if (!Command && !Quiet && Verbose != 1)
  458.         {
  459.             fprintf(STDERR, "sz: %d file%s requested:\n",
  460.                 npats, npats>1?"s":"");
  461.             for ( agcnt=npats, agcv=patts; --agcnt>=0; )
  462.             {
  463.                 fprintf(STDERR, "%s ", *agcv++);
  464.             }
  465.             fprintf(STDERR, "\n\n");
  466.  
  467. #ifdef SDEBUG
  468.             if(Verbose > 2)
  469.             {
  470.                 fprintf(logf, "sz: %d file%s requested:\n",
  471.                     npats, npats>1?"s":"");
  472.                 for ( agcnt=npats, agcv=patts; --agcnt>=0; )
  473.                 {
  474.                     fprintf(logf, "%s ", *agcv++);
  475.                 }
  476.                 fprintf(logf, "\n");
  477.                 fflush(logf);
  478.             }
  479. #endif
  480.         }
  481.         if (!Nozmodem) {
  482.             stohdr(0L);
  483.             if (Command)
  484.                 Txhdr[ZF0] = ZCOMMAND;
  485.             zshhdr(ZRQINIT, Txhdr);
  486.         }
  487.     }
  488.     flush_modem();
  489.  
  490.     if (Command) {
  491.         if (getzrxinit()) {
  492.             Exitcode=0200; canit();
  493.         }
  494.         else if (zsendcmd(Cmdstr, 1+strlen(Cmdstr))) {
  495.             Exitcode=0200; canit();
  496.         }
  497.     } else if (wcsend(npats, patts)==ERROR) {
  498.         Exitcode=0200;
  499.         canit();
  500.     }
  501.     flush_modem();
  502.     mode(0);
  503.  
  504. #ifdef SDEBUG
  505.     if(logf != (FILE *)NULL)
  506.         fclose(logf);
  507. #endif
  508.     if(in != -1)
  509.     {
  510.         fclose(in);
  511.         in = (-1);
  512.     }
  513.  
  514.     putc('\n', STDERR);
  515.     Setexc(2, BusErr);
  516.     Setexc(3, AddrErr);
  517.     Setexc(0x0102, SaveIntr);
  518.     RETURN((errcnt != 0) | Exitcode);
  519.  
  520. }
  521.  
  522. #ifdef STANDALONE
  523. RETURN(n)
  524. int n;
  525. {
  526.     ResetIoBuf();
  527. #if (MWC || MANX)
  528. #ifndef DYNABUF
  529.     free(bufr);
  530. #else
  531.     Mfree(bufr);
  532. #endif
  533. #else
  534. #ifdef DYNABUF
  535.     Mfree(bufr);
  536. #endif
  537. #endif
  538.     exit(n);
  539. }
  540. #endif /* STANDALONE */
  541.  
  542. wcsend(argc, argp)
  543. char *argp[];
  544. {
  545.     register int n;
  546.  
  547.     Crcflg=FALSE;
  548.     Firstsec=TRUE;
  549.     for (n=0; n<argc; ++n) {
  550.         Totsecs = 0;
  551.         if (wcs(argp[n])==ERROR)
  552.             return ERROR;
  553.     }
  554.     Totsecs = 0;
  555.     if (Filcnt==0) {    /* bitch if we couldn't open ANY files */
  556.         if (1) {
  557.             Command = TRUE;
  558.             Cmdstr = "echo \"sz: Can't open any requested files\"";
  559.             if (getzrxinit()) {
  560.                 Exitcode=0200; canit();
  561.             }
  562.             else if (zsendcmd(Cmdstr, 1+strlen(Cmdstr))) {
  563.                 Exitcode=0200; canit();
  564.             }
  565.             Exitcode = 1; return OK;
  566.         }
  567.         canit();
  568.         fprintf(STDERR,"\n\nCan't open any requested files.\n\n");
  569.         return ERROR;
  570.     }
  571.     if (Zmodem)
  572.         saybibi();
  573.     else
  574.         wctxpn("");
  575.     return OK;
  576. }
  577.  
  578. wcs(oname)
  579. char *oname;
  580. {
  581.     extern struct stat statbuf;
  582.     char name[PATHLEN];
  583.  
  584.     strcpy(name, oname);
  585.     ++Noeofseen;  Lastread = 0;  Lastc = (-1); Dontread = FALSE;
  586.  
  587.     /* Check for directory or block special files */
  588.     if(Fsfirst(name,(int)(0x01 | 0x010 | 0x020)) != 0)
  589.     {
  590.         ++errcnt;
  591.         return OK;    /* may be others */
  592.     }
  593.  
  594.     if (statbuf.st_mode & S_IFDIR ) {
  595.         return OK;
  596.     }
  597.  
  598.     if((in = stfopen(oname,"r")) <= 0){
  599.         ++errcnt;
  600.         return OK;    /* pass over it, there may be others */
  601.     }
  602.  
  603.     ++Filcnt;
  604.     switch (wctxpn(name)) {
  605.     case ERROR:
  606.         return ERROR;
  607.     case ZSKIP:
  608.         return OK;
  609.     }
  610.     if (!Zmodem && wctx()==ERROR)
  611.         return ERROR;
  612.     if (Unlinkafter)
  613.         unlink(oname);
  614.     return 0;
  615. }
  616.  
  617. #define ISDRIVE(X) ( (((X >= 'a') && (X <= 'n'))) || ((X >= 'A') && (X <= 'N')))
  618. /*
  619.  * generate and transmit pathname block consisting of
  620.  *  pathname (null terminated),
  621.  *  file length, mode time and file mode in octal
  622.  *  as provided by the St's Fsfirst() call.
  623.  *  N.B.: modifies the passed name, may extend it!
  624.  */
  625. wctxpn(name)
  626. char *name;
  627. {
  628.     register char *p, *q;
  629.     char name2[PATHLEN];
  630.     unsigned long unixtime;
  631.     extern struct stat statbuf;
  632.     extern unsigned long st2unix();    /* Convert St's date and time to unix
  633.                    time (seconds since Jan 1 1970 00:00:00) */
  634.  
  635.     if(*name)
  636.         if(Fsfirst(name,(int)(0x01 | 0x020)) != 0)
  637.             return ERROR;
  638.  
  639.     if (Modem) {
  640. #ifndef REMOTE
  641.         if (*name) {
  642.             fprintf(STDERR,
  643.             "Outgoing:\n\t Name: %s\n\t Size: %ld Bytes\n\
  644. \tBlocks: %ld\n\tBufSize: %ld\n\n",
  645.               name, statbuf.st_size, statbuf.st_size>>7, (long)BBUFSIZ);
  646.         }
  647. #endif /* REMOTE */
  648.         return OK;
  649.     }
  650.  
  651.     vfile2("\r\nAwaiting pathname nak for %s\r\n", *name?name:"<END>");
  652.  
  653.     if ( !Zmodem)
  654.         if (getnak())
  655.             return ERROR;
  656.  
  657.     /* convert to Unix style path names */
  658.     /* skip any device identifier */
  659.     if(ISDRIVE(name[0]) && (name[1] == ':'))
  660.         name = &name[2];
  661.  
  662.     for(p = name; *p != '\0'; p++)
  663.     {
  664.         if(*p == '\\')
  665.             *p = '/';
  666.     }
  667.  
  668.     if(!Resuming)
  669.     {
  670.         if(ForceBin)
  671.         {
  672.             Lzconv = ZCBIN;
  673.             Ascii = FALSE;
  674.         }
  675.         else
  676.         {
  677.             if(!isbinary(name))
  678.             {
  679.                 /* We indicate to the other side */
  680.                 Lzconv = ZCNL;
  681.                 Ascii = TRUE;
  682.             }
  683.             else
  684.             {
  685.                 Lzconv = ZCBIN;
  686.                 Ascii = FALSE;
  687.             }
  688.         }
  689.     }
  690.  
  691.     q = (char *) 0;
  692.     if (Dottoslash) {        /* change . to . */
  693.         for (p=name; *p; ++p) {
  694.             if (*p == '/')
  695.                 q = p;
  696.             else if (*p == '.')
  697.                 *(q=p) = '/';
  698.         }
  699.         if (q && strlen(++q) > 8) {    /* If name>8 chars */
  700.             q += 8;            /*   make it .ext */
  701.             strcpy(name2, q);    /* save excess of name */
  702.             *q = '.';
  703.             strcpy(++q, name2);    /* add it back */
  704.         }
  705.     }
  706.  
  707.     for (p=name, q=secbuf ; *p; )
  708.         if ((*q++ = *p++) == '/' && !Fullname)
  709.             q = secbuf;
  710.     *q++ = 0;
  711.     p=q;
  712.     while (q < (secbuf + KSIZE))
  713.         *q++ = 0;
  714.     if (*name)
  715.     {
  716.         unixtime = st2unix(statbuf.st_time, statbuf.st_date);
  717.         sprintf(p, "%lu %lo %o", statbuf.st_size,
  718.             unixtime,
  719.              ((statbuf.st_mode & 0x01)?0444:0644));
  720.     }
  721.  
  722.     if(Verbose)
  723. #ifndef REMOTE
  724.         fprintf(STDERR,
  725. "Outgoing: [Hit CTRL-C to Cancel]\n\tName: %s\n\tSize: %ld Bytes\n\tBufSize:\
  726.  %ld\n",
  727.             name, statbuf.st_size, (long)BBUFSIZ);
  728.     if(!Resuming)
  729.     {
  730.         fprintf(STDERR,"\tMode: %s\n\n", (Ascii)?"ASCII":"BINARY");
  731.     }
  732.     else
  733.     {
  734.         fprintf(STDERR,"\tMode: Resume Transfer Mode\n\n");
  735.     }
  736. #endif
  737.  
  738. #ifdef SDEBUG
  739.     if(Verbose > 2)
  740.     {
  741.         fprintf(STDERR,"File: %s (%s)\n", name, p);
  742.         fprintf(logf,"File: %s (%s)\n", name, p);
  743.         fflush(logf);
  744.     }
  745. #endif
  746.  
  747.     /* force 1k blocks if name won't fit in 128 byte block */
  748.     if (secbuf[125])
  749.         Blklen=KSIZE;
  750.     else {        /* A little goodie for IMP/KMD */
  751.         if (Zmodem)
  752.             Blklen = SECSIZ;
  753.         secbuf[127] = (statbuf.st_size + 127) >>7;
  754.         secbuf[126] = (statbuf.st_size + 127) >>15;
  755.     }
  756.     if (Zmodem)
  757.         return zsendfile(secbuf, (int)(1+strlen(p)+ 
  758.                 (int)((long)p-(long)secbuf)), statbuf.st_size);
  759.     if (wcputsec(secbuf, 0, SECSIZ)==ERROR)
  760.         return ERROR;
  761.     return OK;
  762. }
  763.  
  764. getnak()
  765. {
  766.     register int firstch;
  767.  
  768.     Lastrx = 0;
  769.     for (;;) {
  770.         switch (firstch = readock(800,1)) {
  771.         case ZPAD:
  772.             if (getzrxinit())
  773.                 return ERROR;
  774.             Ascii = 0;
  775.             return FALSE;
  776.         case TIMEOUT:
  777.             vfile("Timeout on pathname\n");
  778.             return TRUE;
  779.         case WANTG:
  780. #ifdef USG
  781.             mode(2);    /* Set cbreak, XON/XOFF, etc. */
  782. #endif
  783.             Optiong = TRUE;
  784.             Blklen=KSIZE;
  785.         case WANTCRC:
  786.             Crcflg = TRUE;
  787.         case NAK:
  788.             return FALSE;
  789.         case CAN:
  790.             if ((firstch = readock(20,1)) == CAN && Lastrx == CAN)
  791.                 return TRUE;
  792.         default:
  793.             break;
  794.         }
  795.         Lastrx = firstch;
  796.     }
  797. }
  798.  
  799.  
  800. wctx()
  801. {
  802.     register int sectnum, attempts, firstch;
  803.  
  804.     Firstsec=TRUE;
  805.  
  806.     while ((firstch=readock(Rxtimeout, 2))!=NAK && firstch != WANTCRC
  807.       && firstch != WANTG && firstch!=TIMEOUT && firstch!=CAN)
  808.         ;
  809.     if (firstch==CAN) {
  810.         fprintf(STDERR, "\r\nReceiver CANcelled\n");
  811.         return ERROR;
  812.     }
  813.     if (firstch==WANTCRC)
  814.         Crcflg=TRUE;
  815.     if (firstch==WANTG)
  816.         Crcflg=TRUE;
  817.     sectnum=1;
  818.     while (filbuf(secbuf, Blklen)) {
  819.         if (wcputsec(secbuf, sectnum, Blklen)==ERROR) {
  820.             return ERROR;
  821.         } else
  822.             sectnum++;
  823.     }
  824.  
  825. #ifndef REMOTE
  826.     if (Verbose>1)
  827.     {
  828.         fprintf(STDERR, "\nClosing\n\n");
  829. #ifdef SDEBUG
  830.         if(Verbose > 2)
  831.         {
  832.             fprintf(logf, " Closing\n");
  833.             fflush(logf);
  834.         }
  835. #endif
  836.     }
  837. #endif /* REMOTE */
  838.  
  839.     stfclose(in);
  840.     in = (-1);
  841.  
  842.     attempts=0;
  843.     do {
  844.         vfile(" EOT ");
  845.         purgeline();
  846.         sendline(EOT);
  847.         flush_modem();
  848.         ++attempts;
  849.     }
  850.         while ((firstch=(readock(Rxtimeout, 1)) != ACK) && attempts < RETRYMAX);
  851.     if (attempts == RETRYMAX) {
  852.         fprintf(STDERR, "\r\nNo ACK on EOT\n");
  853.         return ERROR;
  854.     }
  855.     else
  856.         return OK;
  857. }
  858.  
  859. wcputsec(buf, sectnum, cseclen)
  860. char *buf;
  861. int sectnum;
  862. int cseclen;    /* data length of this sector to send */
  863. {
  864.     register int checksum, wcj;
  865.     register char *cp;
  866.     unsigned int oldcrc;
  867.     int firstch;
  868.     int attempts;
  869.  
  870.     firstch=0;    /* part of logic to detect CAN CAN */
  871.  
  872. #ifndef REMOTE
  873.     if (Verbose>1)
  874.     {
  875.         fprintf(STDERR, "\rBlock %06d  %04dK ", Totsecs, (Totsecs>>3));
  876. #ifdef SDEBUG
  877.         if(Verbose > 2)
  878.         {
  879.             fprintf(logf, "\rBlock %d %dK ", Totsecs, (Totsecs>>3) );
  880.             fflush(logf);
  881.         }
  882. #endif
  883.     }
  884. #endif /* REMOTE */
  885.  
  886.     for (attempts=0; attempts <= RETRYMAX; attempts++) {
  887.         Lastrx= firstch;
  888.         sendline(cseclen==KSIZE?STX:SOH);
  889.         sendline(sectnum);
  890.         sendline(-sectnum -1);
  891.         oldcrc=checksum=0;
  892.         for (wcj=cseclen,cp=buf; --wcj>=0; ) {
  893.             sendline(*cp);
  894.             oldcrc=updcrc((0377& *cp), oldcrc);
  895.             checksum += *cp++;
  896.         }
  897.         if (Crcflg) {
  898.             oldcrc=updcrc(0,updcrc(0,oldcrc));
  899.             sendline((int)oldcrc>>8);
  900.             sendline((int)oldcrc);
  901.         }
  902.         else
  903.             sendline(checksum);
  904.  
  905.         if (Optiong) {
  906.             Firstsec = FALSE; return OK;
  907.         }
  908.         firstch = readock(Rxtimeout, (Noeofseen&§num) ? 2:1);
  909. gotnak:
  910.         switch (firstch) {
  911.         case CAN:
  912.             if(Lastrx == CAN) {
  913. cancan:
  914.                 fprintf(STDERR, "\r\nCancelled\n");  return ERROR;
  915.             }
  916.             break;
  917.         case TIMEOUT:
  918.             vfile("Timeout on sector ACK\n"); continue;
  919.         case WANTCRC:
  920.             if (Firstsec)
  921.                 Crcflg = TRUE;
  922.         case NAK:
  923.             vfile("NAK on sector\n"); continue;
  924.         case ACK: 
  925.             Firstsec=FALSE;
  926.             Totsecs += (cseclen>>7);
  927.             return OK;
  928.         case ERROR:
  929.             vfile("Got burst for sector ACK\n"); break;
  930.         default:
  931.             vfile("Got %02x for sector ACK\n", firstch); break;
  932.         }
  933.         for (;;) {
  934.             Lastrx = firstch;
  935.             if ((firstch = readock(Rxtimeout, 2)) == TIMEOUT)
  936.                 break;
  937.             if (firstch == NAK || firstch == WANTCRC)
  938.                 goto gotnak;
  939.             if (firstch == CAN && Lastrx == CAN)
  940.                 goto cancan;
  941.         }
  942.     }
  943.     fprintf(STDERR, "\r\nRetry Count Exceeded\n");
  944.     return ERROR;
  945. }
  946.  
  947. /* fill buf with count chars padding with ^Z for CPM */
  948. filbuf(buf, count)
  949. register unsigned char *buf;
  950. register int count;
  951. {
  952.     register int m;
  953.  
  954.     if((buf[0] = stgetc(in)) == EOF)
  955.         return 0;
  956.  
  957.     for(m = 1; (m < count) && ((buf[m] = stgetc(in)) != EOF); m++)
  958.         /* loop */ ;
  959.  
  960.     while (m < count)
  961.         buf[m++] = 032;
  962.  
  963.     return count;
  964.  
  965. }
  966. /* fill buf with count chars */
  967. zfilbuf(buf, count)
  968. register char *buf;
  969. {
  970.     register int c, m;
  971.  
  972.     m=count;
  973.     while ((c=stgetc(in))!=EOF) {
  974.         *buf++ =c;
  975.         if (--m == 0)
  976.             break;
  977.     }
  978.     return (count - m);
  979. }
  980.  
  981. /*
  982.  * readock(timeout, count) reads character(s) from file descriptor 0
  983.  *  (1 <= count <= 3)
  984.  * it attempts to read count characters. If it gets more than one,
  985.  * it is an error unless all are CAN
  986.  * (otherwise, only normal response is ACK, CAN, or C)
  987.  *  Only looks for one if Optiong, which signifies cbreak, not raw input
  988.  *
  989.  * timeout is in tenths of seconds
  990.  */
  991. readock(timeout, count)
  992. int timeout, count;
  993. {
  994.     register int c;
  995.     static char byt[5];
  996.  
  997.     if (Optiong)
  998.         count = 1;    /* Special hack for cbreak */
  999.  
  1000.     if (setjmp(tohere)) {
  1001.         vfile("TIMEOUT\n");
  1002.         return TIMEOUT;
  1003.     }
  1004.     c = timeout >> 3;
  1005.     if (c<2)
  1006.         c=2;
  1007. #ifdef SDEBUG
  1008.     if (Verbose>3) {
  1009.         fprintf(STDERR, "Timeout=%d Calling alarm(%d) ", timeout, c);
  1010.         byt[1] = 0;
  1011.         fprintf(logf, "Timeout=%d Calling alarm(%d) ", timeout, c);
  1012.         fflush(logf);
  1013.  
  1014.     }
  1015. #endif
  1016.     stalarm(c);
  1017.     c=read_modem(byt, count);
  1018.  
  1019.     stalarm(0);
  1020. #ifdef SDEBUG
  1021.     if (Verbose>5)
  1022.     {
  1023.         fprintf(STDERR, "ret cnt=%d %x %x\n", c, byt[0], byt[1]);
  1024.         fprintf(logf, "ret cnt=%d %x %x\n", c, byt[0], byt[1]);
  1025.         fflush(logf);
  1026.     }
  1027. #endif
  1028.  
  1029.     if (c<1)
  1030.         return TIMEOUT;
  1031.     if (c==1)
  1032.         return (byt[0]&0377);
  1033.     else
  1034.         while (c)
  1035.             if (byt[--c] != CAN)
  1036.                 return ERROR;
  1037.     return CAN;
  1038. }
  1039.  
  1040. sreadline(n)
  1041. {
  1042.     return (readock(n, 1));
  1043. }
  1044.  
  1045.  
  1046. susage()
  1047. {
  1048.     fprintf(STDERR,"\nSend file(s) with ZMODEM/YMODEM/XMODEM Protocol\n");
  1049.     fprintf(STDERR,"    (Y) = Option applies to YMODEM only\n");
  1050.     fprintf(STDERR,"    (Z) = Option applies to ZMODEM only\n");
  1051.     fprintf(STDERR,
  1052.     "%s for %s by ST Enthusiasts at Case Western Reserve University\n",
  1053.        SSTVERSION, STOS);
  1054.     fprintf(STDERR,"\tBased on %s for %s by Chuck Forsberg\n\n", SVERSION, OS);
  1055. /*    fprintf(STDERR,"Usage:    sz [-12+adefknquvXy] [-] file ...\n"); */
  1056.     fprintf(STDERR,"Usage:    sz [-+defknquvXyB] file ...\n");
  1057. /*    fprintf(STDERR,"    sz [-1eqv] -c COMMAND\n"); */
  1058.     fprintf(STDERR,"    sz [-eqv] -c COMMAND\n");
  1059. /*    fprintf(STDERR,"    1 Use stdout for modem input\n"); */
  1060. #ifdef CSTOPB
  1061.     fprintf(STDERR,"    2 Use 2 stop bits\n");
  1062. #endif
  1063.     fprintf(STDERR,"    + Append to existing destination file (Z)\n");
  1064. /*    fprintf(STDERR,"    a (ASCII) change NL to CR/LF\n"); */
  1065.     fprintf(STDERR,"    c send COMMAND (Z)\n");
  1066.     fprintf(STDERR,"    d Change '.' to '/' in pathnames (Y/Z)\n");
  1067.     fprintf(STDERR,"    e Escape control characters (Z)\n");
  1068.     fprintf(STDERR,"    f send Full pathname (Y/Z)\n");
  1069.     fprintf(STDERR,"    i send COMMAND, ack Immediately (Z)\n");
  1070.     fprintf(STDERR,"    k Send 1024 byte packets (Y)\n");
  1071.     fprintf(STDERR,"    L N Limit packet length to N bytes (Z)\n");
  1072.     fprintf(STDERR,"    l N Limit frame length to N bytes (l>=L) (Z)\n");
  1073.     fprintf(STDERR,"    n send file if Newer|longer (Z)\n");
  1074.     fprintf(STDERR,"    N send file if different length|date (Z)\n");
  1075.     fprintf(STDERR, "    o Use 16 bit CRC instead of 32 bit CRC (Z)\n");
  1076.     fprintf(STDERR,"    p Protect existing destination file (Z)\n");
  1077.     fprintf(STDERR,"    r Resume/Recover interrupted file transfer (Z)\n");
  1078.     fprintf(STDERR,"    q Quiet (no progress reports)\n");
  1079.     fprintf(STDERR,"    u Unlink file after transmission\n");
  1080.     fprintf(STDERR,"    v Verbose - debugging information\n");
  1081.     fprintf(STDERR,"    X XMODEM protocol - send no pathnames\n");
  1082.     fprintf(STDERR,"    y Yes, overwrite existing file (Z)\n");
  1083.     fprintf(STDERR,"    B Force Binary mode transfers (Z)\n");
  1084. /*    fprintf(STDERR,
  1085. "- as pathname sends standard input as sPID.sz or environment ONAME\n"); */
  1086.     return(1);
  1087. }
  1088.  
  1089. /*
  1090.  * Get the receiver's init parameters
  1091.  */
  1092. getzrxinit()
  1093. {
  1094.     register int n;
  1095.  
  1096.     for (n=10; --n>=0; ) {
  1097.         
  1098.         switch (zgethdr(Rxhdr, 1)) {
  1099.         case ZCHALLENGE:    /* Echo receiver's challenge numbr */
  1100.             stohdr(Rxpos);
  1101.             zshhdr(ZACK, Txhdr);
  1102.             continue;
  1103.         case ZCOMMAND:        /* They didn't see out ZRQINIT */
  1104.             stohdr(0L);
  1105.             zshhdr(ZRQINIT, Txhdr);
  1106.             continue;
  1107.         case ZRINIT:
  1108.             Rxflags = 0377 & Rxhdr[ZF0];
  1109.              Txfcs32 = (Wantfcs32 && (Rxflags & CANFC32));
  1110.  
  1111.             Rxbuflen = (0337 & Rxhdr[ZP0])+((0377 & Rxhdr[ZP1])<<8);
  1112.             vfile("Rxbuflen=%d Tframlen=%d", Rxbuflen, Tframlen);
  1113.             siggi = 0;
  1114. #ifndef READCHECK
  1115. #ifdef USG
  1116.             mode(2);    /* Set cbreak, XON/XOFF, etc. */
  1117. #else
  1118.             /* Use 1024 byte frames if no sample/interrupt */
  1119.             if (Rxbuflen < 32 || Rxbuflen > 1024) {
  1120.                 Rxbuflen = 1024;
  1121.                 vfile("Rxbuflen=%d", Rxbuflen);
  1122.             }
  1123. #endif
  1124. #endif
  1125.             /* Override to force shorter frame length */
  1126.             if (Rxbuflen && (Rxbuflen>Tframlen) && (Tframlen>=32))
  1127.                 Rxbuflen = Tframlen;
  1128.             if ( !Rxbuflen && (Tframlen>=32) && (Tframlen<=1024))
  1129.                 Rxbuflen = Tframlen;
  1130.             vfile("Rxbuflen=%d", Rxbuflen);
  1131.  
  1132.             /*
  1133.              * If input is not a regular file, force ACK's each 1024
  1134.              *  (A smarter strategey could be used here ...)
  1135.              */
  1136.             if ((Rxbuflen == 0) || (Rxbuflen > 1024))
  1137.                 Rxbuflen = 1024;
  1138.             vfile("Rxbuflen=%d", Rxbuflen);
  1139.  
  1140.             return (sendzsinit());
  1141.         case ZCAN:
  1142.         case TIMEOUT:
  1143.             return ERROR;
  1144.         case ZRQINIT:
  1145.             if (Rxhdr[ZF0] == ZCOMMAND)
  1146.                 continue;
  1147.         default:
  1148.             zshhdr(ZNAK, Txhdr);
  1149.             continue;
  1150.         }
  1151.     }
  1152.     return ERROR;
  1153. }
  1154.  
  1155. /* Send send-init information */
  1156. sendzsinit()
  1157. {
  1158.     register int c;
  1159.     register int errors;
  1160.  
  1161.     if (Myattn[0] == '\0')
  1162.         return OK;
  1163.  
  1164.     errors = 0;
  1165.     for (;;) {
  1166.         stohdr(0L);
  1167.         zsbhdr(ZSINIT, Txhdr);
  1168.         zsdata(Myattn, 1+strlen(Myattn), ZCRCW);
  1169.         c = zgethdr(Rxhdr, 1);
  1170.         switch (c) {
  1171.         case ZCAN:
  1172.             return ERROR;
  1173.         case ZACK:
  1174.             return OK;
  1175.         default:
  1176.             if (++errors > 9)
  1177.                 return ERROR;
  1178.             continue;
  1179.         }
  1180.     }
  1181. }
  1182.  
  1183. /* Send file name and related info */
  1184. zsendfile(buf, blen, szbytes)
  1185. char *buf;
  1186. int blen;
  1187. long szbytes;
  1188. {
  1189.     register int c, szstat;
  1190.     long start_time, end_time;
  1191.     extern void rd_time();
  1192.  
  1193.     Supexec(rd_time);
  1194.     start_time = pr_time;
  1195.     
  1196.     for (;;) {
  1197.         Txhdr[ZF0] = Lzconv;    /* file conversion request */
  1198.         Txhdr[ZF1] = Lzmanag;    /* file management request */
  1199.         Txhdr[ZF2] = Lztrans;    /* file transport request */
  1200.         Txhdr[ZF3] = 0;
  1201.         zsbhdr(ZFILE, Txhdr);
  1202.         zsdata(buf, blen, ZCRCW);
  1203. again:
  1204.         c = zgethdr(Rxhdr, 1);
  1205.         switch (c) {
  1206.         case ZRINIT:
  1207.             goto again;
  1208.         case ZCAN:
  1209.         case TIMEOUT:
  1210.         case ZABORT:
  1211.         case ZFIN:
  1212.             return ERROR;
  1213.         case ZSKIP:
  1214. #ifndef REMOTE
  1215.             fprintf(STDERR,"\n\n");
  1216. #endif
  1217.             stfclose(in); in = (-1); return c;
  1218.         case ZRPOS:
  1219.             if(stfseek(in, Rxpos, 0))
  1220.             {
  1221.                 fprintf(STDERR,"\r\nError While Seeking file\n");
  1222.                 return ERROR;
  1223.             }
  1224.             Txpos = Rxpos; Lastc = (-1); Dontread = FALSE;
  1225.             if((szstat =  zsendfdata()) == OK)
  1226.             {
  1227. #ifndef REMOTE
  1228.                 Supexec(rd_time);
  1229.                 end_time = pr_time;
  1230.                 fprintf(STDERR,"%ld Bytes Sent\t\
  1231. Transfer Time %ld secs.\tApprox %ld cps\n\n", szbytes, (end_time - start_time)/200L,
  1232. szbytes/((end_time - start_time)/200L));
  1233. #endif
  1234.             }
  1235.             return szstat;
  1236.         case ERROR:
  1237.         default:
  1238.             continue;
  1239.         }
  1240.     }
  1241. }
  1242.  
  1243. /* Send the data in the file */
  1244. zsendfdata()
  1245. {
  1246.     register int c, c1, e;
  1247.     register int newcnt;
  1248.     register long tcount = 0;
  1249.     static int tleft = 6;    /* Counter for test mode */
  1250.     
  1251.     if (Baudrate > 300)
  1252.         Blklen = 256;
  1253.     if (Baudrate > 2400)
  1254.         Blklen = KSIZE;
  1255. /*    if (Rxbuflen && Blklen>Rxbuflen)  --jrb do not limit block size */
  1256.     if (Rxbuflen)
  1257.         Blklen = Rxbuflen;
  1258.     if (blkopt && Blklen > blkopt)
  1259.         Blklen = blkopt;
  1260.     vfile("Rxbuflen=%d Blklen=%d", Rxbuflen, Blklen);
  1261. somemore:
  1262.     if (setjmp(intrjmp)) {
  1263. waitack:
  1264.         switch (c1 = getinsync()) {
  1265.         default:
  1266.         case ZCAN:
  1267.             fprintf(STDERR,"\r\nReceiver Cancelled Transfer\n\n");
  1268.             stfclose(in);
  1269.             in = (-1);
  1270.             return ERROR;
  1271.         case ZSKIP:
  1272. #ifndef REMOTE
  1273.             fprintf(STDERR,"\r\nReceiver forced SKIP Transfer(1)\n\n");
  1274. #endif
  1275.             stfclose(in);
  1276.             in = (-1);
  1277.             return c1;
  1278.         case ZACK:
  1279.             tcount += c;
  1280.             lreport(tcount);
  1281.             /* fall thru */
  1282.         case ZRPOS:
  1283.             break;
  1284.         case ZRINIT:
  1285.             return OK;
  1286.         }
  1287.     }
  1288.  
  1289.     siggi = 1;
  1290.     newcnt = Rxbuflen;
  1291.     stohdr(Txpos);
  1292.     zsbhdr(ZDATA, Txhdr);
  1293.  
  1294.     /*
  1295.      * Special testing mode.  This should force receiver to Attn,ZRPOS
  1296.      *  many times.  Each time the signal should be caught, causing the
  1297.      *  file to be started over from the beginning.
  1298.      */
  1299. #ifdef TESTATTN
  1300.     if (Testattn) {
  1301.         if ( --tleft)
  1302.             while (tcount < 20000) {
  1303.                 wr_modem(qbf);
  1304.                 tcount += strlen(qbf);
  1305. #ifdef READCHECK
  1306.                 while (Bconstat(1)) {
  1307.                     switch (sreadline(1)) {
  1308.                     case CAN:
  1309.                     case ZPAD:
  1310.                         goto waitack;
  1311.                     }
  1312.                 }
  1313. #endif
  1314.             }
  1315.         siggi = 0; canit();
  1316.         stsleep(3); purgeline(); mode(0);
  1317. #ifdef SDEBUG
  1318.         if(logf != (FILE *)NULL)
  1319.             fclose(logf);
  1320. #endif
  1321.  
  1322. /*        printf("\nsz: Tcount = %ld\n", tcount); */
  1323.         if (tleft) {
  1324.             /* printf("\r\nERROR: Interrupts Not Caught\n"); */
  1325.             aexit(1);
  1326.         }
  1327.         aexit(0);
  1328.     }
  1329. #endif
  1330.  
  1331.     do {
  1332.         if (Dontread) {
  1333.             c = Lastc;
  1334.         } else {
  1335.             c = zfilbuf(secbuf, Blklen);
  1336.             Lastread = Txpos;  Lastc = c;
  1337.         }
  1338. #ifdef SDEBUG
  1339.         if (Verbose > 10)
  1340.             vfile("Dontread=%d c=%d", Dontread, c);
  1341. #endif
  1342.  
  1343.         Dontread = FALSE;
  1344.         if (c < Blklen)
  1345.             e = ZCRCE;
  1346.         else if (Rxbuflen && (newcnt -= c) <= 0)
  1347.             e = ZCRCW;
  1348.         else
  1349.             e = ZCRCG;
  1350.         zsdata(secbuf, c, e);
  1351.         Txpos += c;
  1352.         if(e == ZCRCG)
  1353.         {
  1354.             tcount += c;
  1355.             lreport(tcount);
  1356.         }
  1357.  
  1358.         if (e == ZCRCW)
  1359.             goto waitack;
  1360. #ifdef READCHECK
  1361.         /*
  1362.          * If the reverse channel can be tested for data,
  1363.          *  this logic may be used to detect error packets
  1364.          *  sent by the receiver, in place of setjmp/longjmp
  1365.          *  rdchk(fdes) returns non 0 if a character is available
  1366.          */
  1367.         flush_modem();
  1368.         while (Bconstat(1)) {
  1369.             switch (sreadline(1)) {
  1370.             case CAN:
  1371.             case ZPAD:
  1372.                 zsdata(secbuf, 0, ZCRCE);
  1373.                 goto waitack;
  1374.             }
  1375.         }
  1376. #endif
  1377.     } while (c == Blklen);
  1378.     tcount += c;
  1379.     lreport(tcount);
  1380.     siggi = 0;
  1381.     lsct = 1;
  1382.  
  1383.     for (;;) {
  1384.         stohdr(Txpos);
  1385.         zsbhdr(ZEOF, Txhdr);
  1386.         switch (getinsync()) {
  1387.         case ZACK:
  1388.             continue;
  1389.         case ZRPOS:
  1390.             goto somemore;
  1391.         case ZRINIT:
  1392.             return OK;
  1393.         case ZSKIP:
  1394. #ifndef REMOTE
  1395.             fprintf(STDERR,"\r\nReceiver forced SKIP Transfer(2)\n\n");
  1396. #endif
  1397.             stfclose(in);
  1398.             in = (-1);
  1399.             return c;
  1400.         default:
  1401.             fprintf(STDERR,"\r\nErrors while Send Data\n\n");
  1402.             stfclose(in);
  1403.             in = (-1);
  1404.             return ERROR;
  1405.         }
  1406.     }
  1407. }
  1408.  
  1409. /*
  1410.  * Respond to receiver's complaint, get back in sync with receiver
  1411.  */
  1412. getinsync()
  1413. {
  1414.     register int c;
  1415.  
  1416.     for (;;) {
  1417. #ifdef TESTATTN
  1418.         if (Testattn) {
  1419.             wr_modem("\r\n\n\n***** Signal Caught *****\r\n");
  1420.             Rxpos = 0; c = ZRPOS;
  1421.         } else
  1422. #endif
  1423.  
  1424.             c = zgethdr(Rxhdr, 0);
  1425.         switch (c) {
  1426.         case ZCAN:
  1427.         case ZABORT:
  1428.         case ZFIN:
  1429.         case TIMEOUT:
  1430.             return ERROR;
  1431.         case ZRPOS:
  1432.             if (Lastc >= 0 && Lastread == Rxpos) {
  1433.                 Dontread = TRUE;
  1434.             } else {
  1435.                 /* clearerr(in); */    /* In case file EOF seen */
  1436.                 if(stfseek(in, Rxpos, 0))
  1437.                 {
  1438.                     fprintf(STDERR,"\r\nError While Seeking file\n");
  1439.                     return ERROR;
  1440.                 }
  1441.  
  1442.             }
  1443.             Txpos = Rxpos;
  1444.             return c;
  1445.         case ZACK:
  1446.             return c;
  1447.         case ZRINIT:
  1448.         case ZSKIP:
  1449. #ifndef REMOTE
  1450.             fprintf(STDERR,"\n\n");
  1451. #endif
  1452.             stfclose(in);
  1453.             in = (-1);
  1454.             return c;
  1455.         case ERROR:
  1456.         default:
  1457.             zsbhdr(ZNAK, Txhdr);
  1458.             continue;
  1459.         }
  1460.     }
  1461. }
  1462. /* Say "bibi" to the receiver, try to do it cleanly */
  1463. saybibi()
  1464. {
  1465.     for (;;) {
  1466.         stohdr(0L);
  1467.         zsbhdr(ZFIN, Txhdr);
  1468.         switch (zgethdr(Rxhdr, 0)) {
  1469.         case ZFIN:
  1470.             sendline('O'); sendline('O'); flush_modem();
  1471.         case ZCAN:
  1472.         case TIMEOUT:
  1473.             return;
  1474.         }
  1475.     }
  1476. }
  1477.  
  1478. /* Send command and related info */
  1479. zsendcmd(buf, blen)
  1480. char *buf;
  1481. {
  1482.     register int c, errors;
  1483.     long cmdnum;
  1484.  
  1485. /*    cmdnum = getpid(); */
  1486.     cmdnum = 1;    /* A random # */
  1487.     errors = 0;
  1488.     for (;;) {
  1489.         stohdr(cmdnum);
  1490.         Txhdr[ZF0] = Cmdack1;
  1491.         zsbhdr(ZCOMMAND, Txhdr);
  1492.         zsdata(buf, blen, ZCRCW);
  1493. listen:
  1494.         Rxtimeout = 100;        /* Ten second wait for resp. */
  1495.         c = zgethdr(Rxhdr, 1);
  1496.  
  1497.         switch (c) {
  1498.         case ZRINIT:
  1499.             continue;
  1500.         case ERROR:
  1501.         case TIMEOUT:
  1502.             if (++errors > Cmdtries)
  1503.                 return ERROR;
  1504.             continue;
  1505.         case ZCAN:
  1506.         case ZABORT:
  1507.         case ZFIN:
  1508.         case ZSKIP:
  1509.         case ZRPOS:
  1510.             return ERROR;
  1511.         default:
  1512.             if (++errors > 10)
  1513.                 return ERROR;
  1514.             continue;
  1515.         case ZCOMPL:
  1516.             Exitcode = Rxpos;
  1517.             saybibi();
  1518.             return OK;
  1519.         case ZRQINIT:
  1520.             vfile("******** RZ *******");
  1521. /*            stsystem("rz"); */
  1522.             vfile("******** SZ *******");
  1523.             goto listen;
  1524.         }
  1525.     }
  1526. }
  1527.  
  1528.  
  1529. #ifndef STANDALONE
  1530. /*
  1531.  * If called as sb use YMODEM protocol
  1532.  */
  1533. schkinvok(s)
  1534. char *s;
  1535. {
  1536.     if (s[0]=='s' && s[1]=='b') {
  1537.         Nozmodem = TRUE; Blklen=KSIZE;
  1538.     }
  1539. }
  1540. #endif
  1541.  
  1542. /* -eof- */
  1543.